#-*-Makefile-*- vim:syntax=make
#
# Copyright (c) 2013 OSW. All rights reserved.
# Copyright (c) 2008-2012 the MansOS team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#  * Redistributions of source code must retain the above copyright notice,
#    this list of  conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# Declares configuration options for make. 
# Users can override the configuration options 
# in their project configuration or make file.
#

APP_CONFIG_FILE = $(PROJDIR)/config
PLATFORM_PRECONFIG_FILE = $(SRC)/make/platforms/preconfig.$(PLATFORM)
PLATFORM_POSTCONFIG_FILE = $(SRC)/make/platforms/config.$(PLATFORM)
DEFAULT_CONFIG_FILE = $(SRC)/make/config.default

# The order matters!
-include $(PLATFORM_PRECONFIG_FILE)
-include $(APP_CONFIG_FILE)
include $(DEFAULT_CONFIG_FILE)
-include $(PLATFORM_POSTCONFIG_FILE)

# if BAUDRATE is set, use it as compile-time variable as well
ifneq ($(BAUDRATE),)
CONST_SERIAL_BAUDRATE ?= $(BAUDRATE)
endif


# Let's hope no environment variables start with 'USE_'
# The alternative is to list all option flags explicitly

ALL_USE_FLAGS := $(filter USE_%, $(.VARIABLES))
USE2DEF = $(if $(filter $($(USE)), y yes), $(patsubst %, -D%, $(USE)))
CDEFS_USE := $(foreach USE, $(ALL_USE_FLAGS), $(USE2DEF))
CFLAGS += $(CDEFS_USE)

ALL_CONST_FLAGS := $(filter CONST_%, $(.VARIABLES))
CONST2DEF = $(patsubst CONST_%, -D%=$($(CONST)), $(CONST))
CDEFS_CONST := $(foreach CONST, $(ALL_CONST_FLAGS), $(CONST2DEF))
CFLAGS += $(CDEFS_CONST)

ifeq ($(DEBUG),y)
CFLAGS += -DDEBUG=1
endif

CFLAGS += -DCPU_MHZ=$(CPU_MHZ)
